package com.jiangyg.mall.member.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiangyg.mall.core.exception.BusinessException;
import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.utils.bean.BeanUtils;
import com.jiangyg.mall.db.mybatis.BaseServiceImpl;
import com.jiangyg.mall.member.constant.ColumnConstant;
import com.jiangyg.mall.member.dao.MemberDao;
import com.jiangyg.mall.member.dto.MemberDTO;
import com.jiangyg.mall.member.entity.Member;
import com.jiangyg.mall.member.vo.MemberAuthzInfoVO;
import com.jiangyg.mall.member.vo.MemberVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MemberServiceImpl extends BaseServiceImpl<MemberDao, Member> implements MemberService {

    @Autowired
    public MemberServiceImpl(MemberDao memberDao) {
        super(memberDao);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public Pagination<MemberVO> selectPagination(Pagination<MemberVO> pagination) throws Exception {
        return this.executeSelectPagination(pagination, MemberVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean register(MemberDTO dto) throws Exception {
        // 验证手机号不能重复注册
        {
            final QueryWrapper<Member> wrapper = new QueryWrapper<>();
            wrapper.eq(ColumnConstant.MOBILE, dto.getMobile());
            final Integer count = this.baseMapper.selectCount(wrapper);
            if (count > 0) {
                throw new BusinessException("该手机号已注册！");
            }
        }
        // 验证账号不能重复
        // TODO
        // 保存会员信息
        final Member member = new Member();
        BeanUtils.copyProperties(member, dto);
        member.setPassword(new BCryptPasswordEncoder().encode(dto.getPassword()));
        member.setLevelId(0L);
        member.setCreateTime(new Date());
        return this.baseMapper.insert(member) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public MemberVO selectById(Long memberId) throws Exception {
        return this.executeSelectById(memberId, MemberVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Long> memberIds) {
        return this.baseMapper.deleteBatchIds(memberIds) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public boolean mobileRegistered(String mobile) {
        final QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq(ColumnConstant.MOBILE, mobile);
        return this.baseMapper.selectCount(wrapper) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public MemberAuthzInfoVO selectAuthzInfoByAccount(String account) throws Exception {
        // 根据登录账号查询会员信息
        final QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq(ColumnConstant.MOBILE, account).or().eq(ColumnConstant.USERNAME, account);
        final Member member = this.baseMapper.selectOne(wrapper);
        if (member == null) {
            return null;
        }
        return BeanUtils.transform(member, MemberAuthzInfoVO.class);
    }

}
